/*-----------------------------------------------------------------------------
 * 
 *
 *-----------------------------------------------------------------------------
 */


#include "server/include/stock_server_command.h"
#include "server/include/stock_server.h"
#include "server/include/stock_server_queue.h"
#include "server/database/include/stock_database.h"

static int 
stock_server_show_stock(char *ticker) 
{
    stock_t *stock = stock_search(ticker, 1);

    if (stock == NULL) {
        print("No data received for: %s", ticker);
        return -1;
    } else {

        printf("%8s @ %8s   (%s)\n", 
               stock->ticker, 
               stock_price_string(stock_last_price(stock)), 
               stock_time_string(stock_last_timestamp(stock)));

        return 0;   
    }

}

static int
stock_server_show_stock_command(char **s) 
{
    token_t  token;
    stock_t *stock;
    int      stock_count = 0;

    get_next_token(s, &token);

    while (token.type != T_EOF) {

        if (STREQ(token.text, "summary") == 0) {

        } else {

        }

        stock_server_show_stock(token.text);
        stock_count++;

        get_next_token(s, &token);
    }

    if (stock_count == 0) {

        FOR_ALL_STOCKS(stock) {
            stock_count++;
            printf("%8s @ %8s   (%s) %d total\n", 
                   stock->ticker,
                   stock_price_string(stock_last_price(stock)),
                   stock_time_string(stock_last_timestamp(stock)), stock->data_count);
        }

        printf("\nProcesses %d stock entries\n", stock_count);
    }

    return 0;
}

static int
stock_server_show_command(char **s)
{
    token_t token;
    
    get_next_token(s, &token);

    while (token.type != T_EOF) {

        if (STREQ(token.text, "stock")) {
            
            printf("\n");
            stock_server_show_stock_command(s);
            printf("\n");

        }

        get_next_token(s, &token);
    }

    return 0;
}

static int
stock_server_config_command(char **s)
{
    token_t token;

    get_next_token(s, &token);

    while (token.type != T_EOF) {


        get_next_token(s, &token);
    }


    return 0;
}

static int
stock_server_clear_command(char **s)
{
    token_t token;

    get_next_token(s, &token);

    while (token.type != T_EOF) {

            

        get_next_token(s, &token);
    }

    return 0;
}

static int
stock_server_debug_command(char **s)
{
    token_t token;

    get_next_token(s, &token);

    while (token.type != T_EOF) {


        get_next_token(s, &token);
    }


    return 0;
}

static void
stock_server_display_help() 
{
    printf("\n");
    printf("HELP\n");
    printf("\n");
}

int 
stock_server_command()
{
    token_t token;
    char   *command;

    printf("# ");

    command = get_input_line();
    
    if (command == NULL) {
        return -1;
    }

    get_next_token(&command, &token);

    while (token.type != T_EOF) {
        
        if (STREQ(token.text, "show")) {

            stock_server_show_command(&command);

        } else if (STREQ(token.text, "set")) {
 
            stock_server_config_command(&command);

        } else if (STREQ(token.text, "clear")) {

            stock_server_clear_command(&command);

        } else if (STREQ(token.text, "debug")) {
 
            stock_server_debug_command(&command);

        } else if (STREQ(token.text, "quit") || STREQ(token.text, "exit")) {

            stock_server_shutdown(0);
            return 1;

        } else if (STREQ(token.text, "help") || STREQ(token.text, "?")) {

            stock_server_display_help();

        } 

        get_next_token(&command, &token);
    }
      
    return 0;
}

void 
stock_server_command_loop()
{
    int quit = 0;

    while (!quit) {
        quit = stock_server_command();
    }
}
